import 'package:equatable/equatable.dart';

/// 项目状态枚举
enum ProjectStatus {
  planning('PLANNING', '规划中'),
  inProgress('IN_PROGRESS', '进行中'),
  testing('TESTING', '测试中'),
  completed('COMPLETED', '已完成'),
  cancelled('CANCELLED', '已取消');

  const ProjectStatus(this.code, this.description);
  final String code;
  final String description;

  static ProjectStatus fromCode(String code) {
    return ProjectStatus.values.firstWhere(
      (status) => status.code == code,
      orElse: () => ProjectStatus.planning,
    );
  }
}

/// 优先级枚举
enum Priority {
  high('HIGH', '高'),
  medium('MEDIUM', '中'),
  low('LOW', '低');

  const Priority(this.code, this.description);
  final String code;
  final String description;

  static Priority fromCode(String code) {
    return Priority.values.firstWhere(
      (priority) => priority.code == code,
      orElse: () => Priority.medium,
    );
  }
}

/// 项目数据模型
class ProjectModel extends Equatable {
  final int id;
  final String projectName;
  final String? description;
  final ProjectStatus status;
  final Priority priority;
  final int creatorId;
  final String? creatorName;
  final DateTime? deadline;
  final DateTime createTime;
  final DateTime? updateTime;
  final TodoStats? todoStats;
  final int excelPlanCount;

  const ProjectModel({
    required this.id,
    required this.projectName,
    this.description,
    required this.status,
    required this.priority,
    required this.creatorId,
    this.creatorName,
    this.deadline,
    required this.createTime,
    this.updateTime,
    this.todoStats,
    this.excelPlanCount = 0,
  });

  factory ProjectModel.fromJson(Map<String, dynamic> json) {
    return ProjectModel(
      id: json['id'] as int,
      projectName: json['projectName'] as String,
      description: json['description'] as String?,
      status: ProjectStatus.fromCode(json['status'] as String),
      priority: Priority.fromCode(json['priority'] as String),
      creatorId: json['creatorId'] as int,
      creatorName: json['creatorName'] as String?,
      deadline: json['deadline'] != null
          ? DateTime.parse(json['deadline'] as String)
          : null,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: json['updateTime'] != null
          ? DateTime.parse(json['updateTime'] as String)
          : null,
      todoStats: json['todoStats'] != null
          ? TodoStats.fromJson(json['todoStats'] as Map<String, dynamic>)
          : null,
      excelPlanCount: json['excelPlanCount'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'projectName': projectName,
      'description': description,
      'status': status.code,
      'priority': priority.code,
      'creatorId': creatorId,
      'creatorName': creatorName,
      'deadline': deadline?.toIso8601String(),
      'createTime': createTime.toIso8601String(),
      'updateTime': updateTime?.toIso8601String(),
      'todoStats': todoStats?.toJson(),
      'excelPlanCount': excelPlanCount,
    };
  }

  ProjectModel copyWith({
    int? id,
    String? projectName,
    String? description,
    ProjectStatus? status,
    Priority? priority,
    int? creatorId,
    String? creatorName,
    DateTime? deadline,
    DateTime? createTime,
    DateTime? updateTime,
    TodoStats? todoStats,
    int? excelPlanCount,
  }) {
    return ProjectModel(
      id: id ?? this.id,
      projectName: projectName ?? this.projectName,
      description: description ?? this.description,
      status: status ?? this.status,
      priority: priority ?? this.priority,
      creatorId: creatorId ?? this.creatorId,
      creatorName: creatorName ?? this.creatorName,
      deadline: deadline ?? this.deadline,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      todoStats: todoStats ?? this.todoStats,
      excelPlanCount: excelPlanCount ?? this.excelPlanCount,
    );
  }

  /// 获取项目进度百分比
  double get progressPercentage {
    if (todoStats == null || todoStats!.total == 0) return 0.0;
    return (todoStats!.completed / todoStats!.total) * 100;
  }

  /// 是否已过期
  bool get isOverdue {
    if (deadline == null) return false;
    return deadline!.isBefore(DateTime.now()) &&
        status != ProjectStatus.completed &&
        status != ProjectStatus.cancelled;
  }

  /// 距离截止日期的天数
  int? get daysUntilDeadline {
    if (deadline == null) return null;
    final now = DateTime.now();
    final difference = deadline!.difference(now).inDays;
    return difference;
  }

  @override
  List<Object?> get props => [
        id,
        projectName,
        description,
        status,
        priority,
        creatorId,
        creatorName,
        deadline,
        createTime,
        updateTime,
        todoStats,
        excelPlanCount,
      ];
}

/// 待办任务统计模型
class TodoStats extends Equatable {
  final int total;
  final int completed;
  final int inProgress;
  final int todo;
  final int blocked;

  const TodoStats({
    required this.total,
    required this.completed,
    required this.inProgress,
    required this.todo,
    required this.blocked,
  });

  factory TodoStats.fromJson(Map<String, dynamic> json) {
    return TodoStats(
      total: json['total'] as int,
      completed: json['completed'] as int,
      inProgress: json['inProgress'] as int,
      todo: json['todo'] as int,
      blocked: json['blocked'] as int,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'completed': completed,
      'inProgress': inProgress,
      'todo': todo,
      'blocked': blocked,
    };
  }

  @override
  List<Object?> get props => [total, completed, inProgress, todo, blocked];
}

/// 项目创建请求模型
class ProjectCreateRequest extends Equatable {
  final String projectName;
  final String? description;
  final Priority priority;
  final DateTime? deadline;

  const ProjectCreateRequest({
    required this.projectName,
    this.description,
    this.priority = Priority.medium,
    this.deadline,
  });

  Map<String, dynamic> toJson() {
    return {
      'projectName': projectName,
      'description': description,
      'priority': priority.code,
      'deadline': deadline?.toIso8601String(),
    };
  }

  @override
  List<Object?> get props => [projectName, description, priority, deadline];
}

/// 项目更新请求模型
class ProjectUpdateRequest extends Equatable {
  final String? projectName;
  final String? description;
  final ProjectStatus? status;
  final Priority? priority;
  final DateTime? deadline;

  const ProjectUpdateRequest({
    this.projectName,
    this.description,
    this.status,
    this.priority,
    this.deadline,
  });

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};

    if (projectName != null) json['projectName'] = projectName;
    if (description != null) json['description'] = description;
    if (status != null) json['status'] = status!.code;
    if (priority != null) json['priority'] = priority!.code;
    if (deadline != null) json['deadline'] = deadline!.toIso8601String();

    return json;
  }

  @override
  List<Object?> get props =>
      [projectName, description, status, priority, deadline];
}
